import {
    find,
    list
} from '../../../../api/product'
import {
    list as tagList
} from '../../../../api/tag'
const util = require('../../../../utils/util.js')

let app = getApp();
Component({
    /**
     *组件属性列表
     */
    properties: {
        info: {
            type: Object
        }
    },
    data: {
        tagIds: [],
        tagId: '',
        tags: [],
        pageSize: '1',
        pageNum: 1,
        total:'',
        images: "",
        thumbnail: "",
        name: "",
        subTitle: "",
        price: null,
        promotions:'',
        product: [],
        id:"",
        GIF:{},
        winWidth: 0,
        winHeight: 0,
        concise: false,
        noMore:false
    },
    lifetimes:{
            attached() {
            var _this = this;
            /**
             * 获取当前设备的宽高
             */
            wx.getSystemInfo({
                success: function (res) {
                    _this.setData({
                        winWidth: res.windowWidth,
                        winHeight: res.windowHeight
                    });
                }
            });
            console.info("info", this.data.info)
            if (this.data.info.tagids != null) {
                this.data.tagIds = this.data.info.tagids.split(',')
                for (var i = 0; i < this.data.tagIds.length; i++) {
                    this.data.tagIds[i] = parseInt(this.data.tagIds[i])
                }
            }
            // console.log("www", this.data.tagIds)
            _this.setData({
                tagIds: this.data.tagIds
            })
            // console.log("tagIds", this.data.tagIds)
            this.loadTags()
        }
    },

    created() {
    },
    /**
     * 组件的方法列表
     */
    methods: {
        loadTags() {
            var _this = this;
            console.log("ss", _this.data.tagIds)
            tagList({
                pageNum: this.data.pageNum,
                pageSize: this.data.pageSize,
                mchId: app.globalData.mchId,
                type: '3',
                tagIds: this.data.tagIds[0],
            }).then(res => {
                _this.data.tags = res.data.data
                _this.data.tagId = this.data.tags[0].id
                // console.log("tagId", _this.data.tagId)
                _this.setData({
                    tagId: _this.data.tagId,
                    tags: _this.data.tags
                })
                console.log("setData", _this.data.tagId, _this.data.tags)
                _this.loadProduct()
            })
        },

        loadProduct(callback) {
            var _this = this;
            if (this.data.info.rows != null) {
              var pageSize = this.data.info.rows
            }
            _this.data.productType = false;
            list({
                tagIds: [this.data.tagId],
                mchId: app.globalData.mchId,
                pageNum: this.data.pageNum,
                pageSize: pageSize,
            }).then(res => {
                console.log("loadProduct-秒杀", res);
                if (res.data.data.length > 0) {
                    res.data.data.forEach(item => {
                      if(item.promotions.length > 0 || item.coupons.length > 0){
                        this.setData({
                          concise:!this.data.concise
                        })
                      }
                      item.thumbnail = util.thumbnail(item.thumbnail, 500, 300)
                     _this.data.product.push(item);
                    });
                    console.log("---秒杀---", _this.data.product);
                    _this.data.total = res.data.total
                    _this.setData({
                        product:this.data.product,
                    })
                }else{
                  this.data.noMore = true
                }
                if (callback) {
                    callback(res.data.data)
                }
                // this.loadMore();
            })

        },

        // loadMore(){
        //     var _this = this;
        //     // console.log("id",this.data.id);
        //     var gifData = {};
        //     if(!util.isNull(this.data.id)){
        //         find(_this.data.id).then(res=>{
        //             if (res.type == "success") {
        //                 // console.log("loadMore-res",res);
        //                 gifData = res.data.meta
        //                 // console.log("gifData",gifData,Object.entries(gifData));
        //             }
        //             // console.log("gifData1",Object.keys(gifData)[1]=="images");
        //             // if( gifData.images.length>0 && Object.keys(gifData)[1] ){
        //             //     console.log("images",Object.keys(gifData)[1],Object.values(gifData));
        //             //     gifData.images = Object.values(gifData)[1]
        //             // }
        //             // if (gifData.video!="" && Object.keys(gifData)[0]=="video" ) {
        //             //     console.log("video",Object.keys(gifData)[0]);
        //             //     gifData.video = Object.values(gifData)[0]  
        //             //  }
        //             var img = gifData.images[0]
        //             gifData.images = util.thumbnail(img, 712, 712)
        //             console.log("img",img,gifData.images);
        //             _this.setData({
        //                 GIF:gifData
        //             })
        //             console.log("GIF",gifData,this.data.GIF);
        //         });
        //     }
        // },
        submitClick(data) {
          console.log('id',data.currentTarget.dataset.id)
            if (app.globalData.token == null) {
              wx.navigateTo({
                url: '/pages/login/index',
              })
            } else {
              wx.navigateTo({
                url: '/pages/goods/product/details?id=' + data.currentTarget.dataset.id,
              })
            }
          },
          productClick(data) {
            wx.navigateTo({
              url: '/pages/goods/product/details?id=' + data.currentTarget.dataset.id,
            })
          },

    
      // +++++++++++上拉加载+++++++++
    onReachBottom: function () {
      let _this = this
      console.info("上拉加载被触发了")
      if(_this.data.product.length < this.data.total &&  !this.data.noMore){
        let pageNum = _this.data.pageNum
        pageNum++;
        _this.setData({
          pageNum: pageNum
        })
        console.info("当前页面数",_this.data.pageNum)
       this.loadProduct()
      }else{
        wx.showToast({
          title: '没有更多了哦！',
          icon:'none',
          duration: 3000
        })
      }
    },
    
    }
})